package com.mcintyret.site.users.database;

import com.mcintyret.site.users.User;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: mcintyret2
 * Date: 07/04/2012
 */

@Repository
public class UsersDao{

    private static final Logger LOG = LoggerFactory.getLogger(UsersDao.class);

    @Resource(name = "usersSessionFactory")
    private SessionFactory sessionFactory;

    public void createNewUser(User newUser) {
        sessionFactory.getCurrentSession().persist(newUser);
    }

    public void deleteUser(User user) {
        sessionFactory.getCurrentSession().delete(user);
    }

    public User retrieveUser(String username) {
        return (User) sessionFactory.getCurrentSession().get(User.class, username);
    }

    public List<User> getAllUsers() {
        return sessionFactory.getCurrentSession().createQuery("from User").list();
    }

}
